var app=getApp();
var fakedata=require("../../static/js/fakedata");
Page({
    data: {
        url:'../../static/images/icon_status-dot.png',
        displayList:[],
        userid:''
    },
    gotodetail(e){
        let goodid=e.currentTarget.dataset.goodid;
        swan.navigateTo({
            url:"/pages/detail/detail?goodid="+goodid
        });
    },
    // 取消交易
    cancledeal(e){
        let goodid=e.currentTarget.dataset.goodid;
        swan.request({
            url: fakedata.urls.cancledeal,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'json',
            data: {
                goodid:goodid
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    swan.showModal({
                        title: '提示',
                        content: '交易已经取消！'
                    });
                    this.updata();
                }else{
                    swan.showModal({
                        title:'错误'
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    },
    onLoad(){
        if(app.globalData.islogin){
            this.setData({
                userid:app.globalData.userinfo.id
            });
        }
    },
    onShow(){
        if(!app.globalData.islogin){
            swan.showModal({
                title:'您还没有登录'
            });
            swan.navigateTo({
                url:'/pages/login/login'
            });
        }else{
            this.setData({
                userid:app.globalData.userinfo.id
            });
            this.updata();
        }
    },
    updata(){
        swan.request({
            url: fakedata.urls.getmybuy,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            data: {
                userid:this.data.userid
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    this.setData({
                        displayList:res.data.data
                    });
                }else{
                    swan.showModal({
                        title:"出错啦"
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    }
});